package com.freesky.web.manager.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.freesky.common.dao.entity.SystemLog;
import com.freesky.common.enums.ResponseEnum;
import com.freesky.common.exception.MyException;
import com.freesky.common.pojo.DataTablesResult;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.web.manager.service.ISystemLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 16:06 2019-01-11
 * @description:
 */
@Service
@Slf4j
public class SystemLogServiceImpl implements ISystemLogService {
    @Override
    public DataTablesResult getLogList(int draw, int start, int length, String search) {
        DataTablesResult result = new DataTablesResult();
        search = "%" + search + "%";
        Page page = new Page(start / length + 1, length);
        Page<SystemLog> logPage = new SystemLog().selectPage(new Page<>(start / length + 1, length), new EntityWrapper<SystemLog>()
                .like("id", search).or()
                .like("username", search).or()
                .like("operation", search).or()
                .like("ip", search).or()
                .like("response_time", search).or()
                .like("create_time", search)
                .orderBy("create_time",false));

        result.setRecordsFiltered(logPage.getTotal());
        result.setRecordsTotal(logPage.getSize());

        result.setDraw(draw);
        result.setData(logPage.getRecords());
        return result;
    }

    @Override
    public JSONResponse countLog() {
        return JSONResponseDiretor.buildSuccessJSONResponse(new SystemLog().selectCount(new EntityWrapper()));
    }

    @Override
    public JSONResponse deleteLog(int[] ids) {
        try {
            for (int id:ids) {
                SystemLog systemLog = new SystemLog(id);
                systemLog.deleteById();
            }
            return JSONResponseDiretor.buildSuccessJSONResponse(null);
        } catch (Exception e) {
            throw new MyException(ResponseEnum.DELETE_SYSTEM_LOG_ERROR, e, null);
        }
    }
}
